package com.pzh.xzh.uaa.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pzh.xzh.uaa.mapper.UserMapper;
import com.pzh.xzh.uaa.pojo.LoginUserDetail;
import com.pzh.xzh.uaa.pojo.User;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class UserDetailServiceImp implements UserDetailsService {
    @Resource
    private UserMapper userMapper;
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
        userQueryWrapper.eq("username",s);
        User user = userMapper.selectOne(userQueryWrapper);
        System.out.println(user.getUsername());
        if (ObjectUtils.isEmpty(user)){
            throw new UsernameNotFoundException("用户不存在");
        }
        return loginUserDetail(user);
    }

    LoginUserDetail loginUserDetail(User user){
         LoginUserDetail loginUserDetail=new LoginUserDetail();
         String authorities="xzh";
         loginUserDetail.setAuthorities((List<GrantedAuthority>) AuthorityUtils.createAuthorityList(authorities));
         loginUserDetail.setUsername(user.getUsername());
         loginUserDetail.setPassword(user.getPassword());
         return loginUserDetail;
    }

}
